package com.java.test.activity.activitypush;

import com.java.test.activity.activitypush.dto.AddActivityDTO;
import com.java.test.dto.ResponseDTO;
import com.java.test.messagecenter.messagepush.dto.SendMailDTO;
import com.java.test.util.RequestGameServiceUtil;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Base64;
import java.util.Date;
import java.util.Random;

/**
 * 活动推送Service
 *
 * @author lijie1
 * @date 2020/7/10 0010
 */
@Slf4j
@Service
public class ActivityPushService {
    @Value("${get.game.offLineUser.url}")
    private String offLineUserUrl;

    public ResponseDTO<String> addActivity(AddActivityDTO dto) {
        String time = new Date().getTime()+"";
        String base64Sign = new String(Base64.getEncoder().encode(dto.getContent().getBytes()));
        String data = " {\"code\":4,\"data\":[\""+dto.getTitle()+"\",\""+base64Sign+"\","+dto.getStartTime()+","+dto.getEndTime()+",\""+dto.getHeadUrl()+"\","+dto.getType()+",\""+dto.getUrl()+"\"]}";
        Random r1 = new Random();
        String url =  RequestGameServiceUtil.createUrl(time,r1.nextInt(100)+"",data,offLineUserUrl);
        log.info(url);
        JSONObject resMsg = RequestGameServiceUtil.getService(url);
        String state = (resMsg.get("state").toString());
        if("0".equals(state)){
            return ResponseDTO.succ();
        }else {
            return ResponseDTO.error(resMsg.get("message").toString());
        }
    }
}
